package com.examsage.examsage.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.examsage.examsage.model.MajorPlan;
import com.examsage.examsage.mapper.MajorPlanMapper;
import com.examsage.examsage.model.vo.ResponseVO;
import com.examsage.examsage.service.IMajorPlanService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author examsage
 * @since 2025-07-02
 */
@Service
public class MajorPlanServiceImpl extends ServiceImpl<MajorPlanMapper, MajorPlan> implements IMajorPlanService {

    @Resource
    private MajorPlanMapper majorPlanMapper;



    @Override
    public ResponseVO addMajorPlan(MajorPlan majorPlan) {
        // TODO  新增专业计划
        majorPlan.setGmtCreate(LocalDateTime.now());
        majorPlan.setGmtModified(LocalDateTime.now());
        majorPlan.setStatus(true);
        ResponseVO responseVO = new ResponseVO();
        if(majorPlanMapper.insert(majorPlan) > 0){
            responseVO.setStatusCode(200);
            responseVO.setMessage("新增专业计划成功");
            responseVO.setData(majorPlan);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("新增专业计划失败");
            responseVO.setData(null);
        }
        return responseVO;
    }


    @Override
    public ResponseVO selectMajorPlan(int page, int size, String majorName, Boolean status) {
        //  TODO 查询专业计划列表（支持分页和条件查询）
        // 构建查询条件
        QueryWrapper<MajorPlan> queryWrapper = new QueryWrapper<>();

        // 按专业名称模糊查询
        if (majorName != null && !majorName.trim().isEmpty()) {
            queryWrapper.like("major_name", majorName);
        }

        // 按状态筛选（1:启用，0:停用）
        if (status != null) {
            queryWrapper.eq("status", status);
        }

        // 分页查询
        com.baomidou.mybatisplus.extension.plugins.pagination.Page<MajorPlan> pageQuery
                = new com.baomidou.mybatisplus.extension.plugins.pagination.Page<>(page, size);

        ResponseVO responseVO = new ResponseVO();

        try {
            // 执行分页查询
            com.baomidou.mybatisplus.extension.plugins.pagination.Page<MajorPlan> resultPage
                    = majorPlanMapper.selectPage(pageQuery, queryWrapper);

            responseVO.setStatusCode(200);
            responseVO.setMessage("查询专业计划成功");
            responseVO.setData(resultPage);
        } catch (Exception e) {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询专业计划失败");
            responseVO.setData(null);
        }

        return responseVO;
    }

    @Override
    public ResponseVO selectOneMajorPlan(Long id){
        ResponseVO responseVO = new ResponseVO();
        MajorPlan majorPlan = majorPlanMapper.selectById(id);
        if(majorPlan != null){
            responseVO.setStatusCode(200);
            responseVO.setMessage("查询专业计划成功");
            responseVO.setData(majorPlan);
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("查询专业计划失败");
            responseVO.setData(null);
        }
        return responseVO;
    }


    @Override
    public ResponseVO updateMajorPlan(Long id, MajorPlan majorPlan) {
        UpdateWrapper<MajorPlan> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("id", id);

        if (majorPlan.getMajorName() != null && !majorPlan.getMajorName().trim().isEmpty()) {
            updateWrapper.set("major_name", majorPlan.getMajorName());
        }
        if (majorPlan.getMajorCode() != null && !majorPlan.getMajorCode().trim().isEmpty()) {
            updateWrapper.set("major_code", majorPlan.getMajorCode());
        }
        if (majorPlan.getDegreeType() != null && !majorPlan.getDegreeType().trim().isEmpty()) {
            updateWrapper.set("degree_type", majorPlan.getDegreeType());
        }
        if (majorPlan.getTotalCredits() != null) {
            updateWrapper.set("total_credits", majorPlan.getTotalCredits());
        }
        if (majorPlan.getStatus() != null) {
            updateWrapper.set("status", majorPlan.getStatus());
        }
        if (majorPlan.getRemark() != null && !majorPlan.getRemark().trim().isEmpty()) {
            updateWrapper.set("remark", majorPlan.getRemark());
        }

        // 自动填充 gmt_modified（如果已配置 MetaObjectHandler）
        ResponseVO responseVO = new ResponseVO();
        if (majorPlanMapper.update(null, updateWrapper) > 0) {
            responseVO.setStatusCode(200);
            responseVO.setMessage("修改专业计划成功");
            responseVO.setData(majorPlanMapper.selectById(id));
        } else {
            responseVO.setStatusCode(500);
            responseVO.setMessage("修改专业计划失败");
            responseVO.setData(null);
        }
        return responseVO;
    }

    @Override
    public ResponseVO deleteMajorPlanById(Long id){
        ResponseVO responseVO = new ResponseVO();
        if(majorPlanMapper.deleteById(id) > 0){
            responseVO.setStatusCode(204);
            responseVO.setMessage("删除专业计划成功");
            responseVO.setData(null);
        } else {
            responseVO.setStatusCode(404);
            responseVO.setMessage("专业不存在");
            responseVO.setData(null);
        }
        return responseVO;
    }


}
